﻿using System;
using WikiPlex.Common;
using WikiPlex.Formatting;

namespace WikiMaml.Renderers
{
    public class TopicLinkRenderer : IRenderer
    {
        /// <summary>
        /// Gets the id of a renderer.
        /// </summary>
        public string Id
        {
            get { return "Topic Link Renderer"; }
        }

        /// <summary>
        /// Determines if this renderer can expand the given scope name.
        /// </summary>
        /// <param name="scopeName">The scope name to check.</param>
        /// <returns>
        /// A boolean value indicating if the renderer can or cannot expand the macro.
        /// </returns>
        public bool CanExpand(string scopeName)
        {
            return (scopeName == MamlScopeName.TopicLink
                    || scopeName == MamlScopeName.TopicLinkNoText);
        }

        /// <summary>
        /// Will expand the input into the appropriate content based on scope.
        /// </summary>
        /// <param name="scopeName">The scope name.</param><param name="input">The input to be expanded.</param><param name="htmlEncode">Function that will html encode the output.</param><param name="attributeEncode">Function that will html attribute encode the output.</param>
        /// <returns>
        /// The expanded content.
        /// </returns>
        public string Expand(string scopeName, string input, Func<string, string> htmlEncode, Func<string, string> attributeEncode)
        {
            switch (scopeName)
            {
                case MamlScopeName.TopicLinkNoText:
                    return string.Format("<link xlink:href=\"{0}\" />", attributeEncode(input));
                case MamlScopeName.TopicLink:
                    TextPart parts = Utility.ExtractTextParts(input);
                    return string.Format("<link xlink:href=\"{0}\">{1}</link>", attributeEncode(parts.Text), htmlEncode(parts.FriendlyText));
                default:
                    return input;
            }
        }
    }
}
